<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="订单编号" prop="orderNo">
        <el-input v-model="formData.orderNo" placeholder="请输入订单编号" />
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="formData.orderType" placeholder="请选择订单类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="用户编号" prop="userId">
        <el-input v-model="formData.userId" placeholder="请输入用户编号" />
      </el-form-item>
      <el-form-item label="用户ip" prop="userIp">
        <el-input v-model="formData.userIp" placeholder="请输入用户ip" />
      </el-form-item>
      <el-form-item label="用户备注" prop="userRemark">
        <el-input v-model="formData.userRemark" placeholder="请输入用户备注" />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="取消类型" prop="cancelType">
        <el-select v-model="formData.cancelType" placeholder="请选择取消类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="是否支付" prop="payed">
        <el-radio-group v-model="formData.payed">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="支付时间" prop="payTime">
        <el-date-picker
          v-model="formData.payTime"
          type="date"
          value-format="x"
          placeholder="选择支付时间"
        />
      </el-form-item>
      <el-form-item label="完成时间" prop="finishTime">
        <el-date-picker
          v-model="formData.finishTime"
          type="date"
          value-format="x"
          placeholder="选择完成时间"
        />
      </el-form-item>
      <el-form-item label="取消时间" prop="cancelTime">
        <el-date-picker
          v-model="formData.cancelTime"
          type="date"
          value-format="x"
          placeholder="选择取消时间"
        />
      </el-form-item>
      <el-form-item label="原价" prop="originalPrice">
        <el-input v-model="formData.originalPrice" placeholder="请输入原价" />
      </el-form-item>
      <el-form-item label="订单价格" prop="orderPrice">
        <el-input v-model="formData.orderPrice" placeholder="请输入订单价格" />
      </el-form-item>
      <el-form-item label="调整价格" prop="adjustPrice">
        <el-input v-model="formData.adjustPrice" placeholder="请输入调整价格" />
      </el-form-item>
      <el-form-item label="支付价格" prop="payPrice">
        <el-input v-model="formData.payPrice" placeholder="请输入支付价格" />
      </el-form-item>
      <el-form-item label="支付订单编号" prop="payOrderId">
        <el-input v-model="formData.payOrderId" placeholder="请输入支付订单编号" />
      </el-form-item>
      <el-form-item label="支付渠道编号" prop="payChannelCode">
        <el-input v-model="formData.payChannelCode" placeholder="请输入支付渠道编号" />
      </el-form-item>
      <el-form-item label="开通状态" prop="deliveryStatus">
        <el-radio-group v-model="formData.deliveryStatus">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="开通时间" prop="deliveryTime">
        <el-date-picker
          v-model="formData.deliveryTime"
          type="date"
          value-format="x"
          placeholder="选择开通时间"
        />
      </el-form-item>
      <el-form-item label="生效时间" prop="receiveTime">
        <el-date-picker
          v-model="formData.receiveTime"
          type="date"
          value-format="x"
          placeholder="选择生效时间"
        />
      </el-form-item>
      <el-form-item label="退款金额" prop="refundPrice">
        <el-input v-model="formData.refundPrice" placeholder="请输入退款金额" />
      </el-form-item>
      <el-form-item label="sku编号" prop="clientSkuId">
        <el-input v-model="formData.clientSkuId" placeholder="请输入sku编号" />
      </el-form-item>
      <el-form-item label="购买数量" prop="buyCount">
        <el-input v-model="formData.buyCount" placeholder="请输入购买数量" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import * as OrderApi from '@/api/client-mall/trade/order'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  orderNo: undefined,
  orderType: undefined,
  userId: undefined,
  userIp: undefined,
  userRemark: undefined,
  status: undefined,
  cancelType: undefined,
  remark: undefined,
  payed: undefined,
  payTime: undefined,
  finishTime: undefined,
  cancelTime: undefined,
  originalPrice: undefined,
  orderPrice: undefined,
  adjustPrice: undefined,
  payPrice: undefined,
  payOrderId: undefined,
  payChannelCode: undefined,
  deliveryStatus: undefined,
  deliveryTime: undefined,
  receiveTime: undefined,
  refundPrice: undefined,
  clientSkuId: undefined,
  buyCount: undefined
})
const formRules = reactive({
  orderNo: [{ required: true, message: '订单编号不能为空', trigger: 'blur' }],
  orderType: [{ required: true, message: '订单类型不能为空', trigger: 'change' }],
  userId: [{ required: true, message: '用户编号不能为空', trigger: 'blur' }],
  userIp: [{ required: true, message: '用户ip不能为空', trigger: 'blur' }],
  userRemark: [{ required: true, message: '用户备注不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '订单状态不能为空', trigger: 'blur' }],
  cancelType: [{ required: true, message: '取消类型不能为空', trigger: 'change' }],
  remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }],
  payed: [{ required: true, message: '是否支付不能为空', trigger: 'blur' }],
  originalPrice: [{ required: true, message: '原价不能为空', trigger: 'blur' }],
  orderPrice: [{ required: true, message: '订单价格不能为空', trigger: 'blur' }],
  adjustPrice: [{ required: true, message: '调整价格不能为空', trigger: 'blur' }],
  payPrice: [{ required: true, message: '支付价格不能为空', trigger: 'blur' }],
  payOrderId: [{ required: true, message: '支付订单编号不能为空', trigger: 'blur' }],
  payChannelCode: [{ required: true, message: '支付渠道编号不能为空', trigger: 'blur' }],
  deliveryStatus: [{ required: true, message: '开通状态不能为空', trigger: 'blur' }],
  refundPrice: [{ required: true, message: '退款金额不能为空', trigger: 'blur' }],
  clientSkuId: [{ required: true, message: 'sku编号不能为空', trigger: 'blur' }],
  buyCount: [{ required: true, message: '购买数量不能为空', trigger: 'blur' }]
})
const formRef = ref({}) // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await OrderApi.getOrder(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef.value) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as OrderApi.OrderVO
    if (formType.value === 'create') {
      await OrderApi.createOrder(data)
      message.success(t('common.createSuccess'))
    } else {
      await OrderApi.updateOrder(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    orderNo: undefined,
    orderType: undefined,
    userId: undefined,
    userIp: undefined,
    userRemark: undefined,
    status: undefined,
    cancelType: undefined,
    remark: undefined,
    payed: undefined,
    payTime: undefined,
    finishTime: undefined,
    cancelTime: undefined,
    originalPrice: undefined,
    orderPrice: undefined,
    adjustPrice: undefined,
    payPrice: undefined,
    payOrderId: undefined,
    payChannelCode: undefined,
    deliveryStatus: undefined,
    deliveryTime: undefined,
    receiveTime: undefined,
    refundPrice: undefined,
    clientSkuId: undefined,
    buyCount: undefined
  }
  formRef.value?.resetFields()
}
</script>
